package ru.home.shoppinglist;

import java.util.Date;

import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.Menu;
import android.view.View;

import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class EditGoodsActivity extends Activity {
	protected static final String LOG_TAG = "EditGoodsActivity";
	//int priorityCode = -1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_edit_goods);
		String[] data = { "None", "Low", "Medium", "High" };
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, data);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		Spinner spinner = (Spinner) findViewById(R.id.spPriority);
		spinner.setAdapter(adapter);
		// заголовок
		spinner.setPrompt("Title");
		// выделяем элемент
		spinner.setSelection(2);
		// устанавливаем обработчик нажатия
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				Log.d(LOG_TAG, position + "");
				// показываем позиция нажатого элемента
				Toast.makeText(getBaseContext(), "Position = " + position,
						Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}

		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.edit_goods, menu);
		return true;
	}
	
	public void onAddRecordClick(View view) {
		EditText goodsName = (EditText) findViewById(R.id.edName);
		DBHelper dbh = new DBHelper(this);
		SQLiteDatabase db = dbh.getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put("name", goodsName.getText().toString());
		cv.put("creation_date", new Date().toString());
		Spinner spinner = (Spinner) findViewById(R.id.spPriority);
		cv.put("priority",spinner.getSelectedItemPosition());
		DatePicker dp=(DatePicker) findViewById(R.id.dpToDate);
		cv.put("to_date",dp.toString());
		long id = getIntent().getLongExtra("category_id", -1);
		if (id == -1) {
			long goodsid=db.insert("Goods", null, cv);
			
		} else {
			db.update("Goods", cv, "id=" + id, null);
		}
		db.close();
		dbh.close();
		finish();
	}

	public void onDeleteClick(View view) {		
		DBHelper dbh = new DBHelper(this);
		SQLiteDatabase db = dbh.getWritableDatabase();		
		long id = getIntent().getLongExtra("category_id", -1);
		if (id == -1) {		
		} else {
			db.delete("Classification", "id=" + id, null);
		}
		db.close();
		dbh.close();
		finish();
	}

	public void onCancelClick(View view) {

		finish();
	}

}
